// pages/login/login.js
import Toast from '@vant/weapp/toast/toast';
import {
    checkPhone
} from '../../utils/reg.js'
import {
    request
} from '../../utils/api.js'
Page({
    data: {
        phone: '17635564815',
        userPwd: '123456',
        message: [],
        sendMessage: "发送验证码",
        disabled: false,
        active: 0,
        loginType: 0,
        code: '',
    },
    onChange(event) {
        this.setData({
            message: []
        })
        this.setData({
            loginType: event.detail.name
        })
    },
    register() {
        wx.navigateTo({
            url: '/pages/login/register/register',
        })
    },

    async send() {
        this.setData({
            message: []
        })
        let reg = checkPhone(this.data.phone)
        if (reg !== true) {
            this.setData({
                [`message[0]`]: reg
            })
            return
        }
        let res = await request({
            url: '/public/sendPhoneCode?phone=' + this.data.phone,
            method: 'get'
        })
        if (res.code == 200) {
            Toast("验证码发送成功")
            let time = 60
            this, this.setData({
                disabled: true
            })
            setInterval(() => {
                if (time > 0) {
                    time--
                    this.setData({
                        sendMessage: time + '秒后继续发送'
                    })
                } else {
                    this.setData({
                        disabled: false,
                        sendMessage: '发送验证码'
                    })
                }
            }, 1000);
        } else {
            Toast(res.message)
        }
    },

    async submit() {
        this.setData({
            message: [],
        })
        let reg = checkPhone(this.data.phone)
        if (reg !== true) {
            this.setData({
                [`message[0]`]: reg
            })
        }
        if (this.data.loginType == '0') {
            if (!this.data.userPwd) this.setData({
                ['message[1]']: '请输入密码'
            })
        } else {
            if (!this.data.code) this.setData({
                ['message[2]']: '请输入验证码'
            })
        }
        if (this.data.message.length === 0) {
            let res = await request({
                url: '/public/login',
                method: 'post',
                data: this.data
            })
            if (res.code == 200) {
                wx.setStorageSync('token', res.result)
                wx.setStorageSync('phone', this.data.phone)
                Toast("登录成功")
                let result = await request({
                    url: '/user/getUserInfo?phone=' + this.data.phone,
                    method: 'get'
                })
                result.result.token = res.result
                wx.setStorageSync('user', result.result)
                setTimeout(() => {
                    wx.switchTab({
                        url: '/pages/person/person',
                    })
                }, 500);
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})